package com.wangxue.stock.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.wangxue.stock.common.domain.*;
import com.wangxue.stock.config.StockInfoConfig;
import com.wangxue.stock.common.enums.ResponseCode;
import com.wangxue.stock.mapper.StockBlockRtInfoMapper;
import com.wangxue.stock.mapper.StockBusinessMapper;
import com.wangxue.stock.mapper.StockMarketIndexInfoMapper;
import com.wangxue.stock.mapper.StockRtInfoMapper;
import com.wangxue.stock.pojo.StockBlockRtInfo;
import com.wangxue.stock.pojo.StockBusiness;
import com.wangxue.stock.service.StockService;
import com.wangxue.stock.utils.DateTimeUtil;
import com.wangxue.stock.vo.resp.PageResult;
import com.wangxue.stock.vo.resp.R;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 86198
 * @Date 2022/05/16
 * 实现股票服务接口
 */
@Service("stockService")
@Slf4j //log.info打印日志
public class StockServiceImpl implements StockService {
    @Autowired
    private StockBusinessMapper stockBusinessMapper;

    @Autowired
    private StockMarketIndexInfoMapper stockMarketIndexInfoMapper;

    @Autowired
    private StockInfoConfig stockInfoConfig;

    @Autowired
    private StockBlockRtInfoMapper stockBlockRtInfoMapper;

    @Autowired
    private StockRtInfoMapper stockRtInfoMapper;


    @Override
    public List<StockBusiness> getAllStockBusiness() {
        return this.stockBusinessMapper.getAll();
    }
    /**
     * 获取国内大盘的实时数据
     *
     * @return  R<List<InnerMarketDomain>>
     * @author wangxue
     * @Date 2022/5/20
     */
    @Override
    public R<List<InnerMarketDomain>> innerIndexAll() {

        //1.获取国内大盘的id集合
        List<String> innerIds = stockInfoConfig.getInner();
        //2.获取最近最新的股票有效交易日
        Date lDate = DateTimeUtil.getLastDate4Stock(DateTime.now()).toDate();
        //mock数据
        String mockDate = "20211226105600"; //后续大盘数据实时拉取，将该行注释掉，传入的日期秒必须为0
        lDate = DateTime.parse(mockDate, DateTimeFormat.forPattern("yyyyMMddHHmmss")).toDate();
        //3.调用mapper查询指定日期下对应的国内大盘数据
        List<InnerMarketDomain> maps = stockMarketIndexInfoMapper.selectByIdsAndDate(innerIds,lDate);
        //组装相应的额数据
        if(CollectionUtils.isEmpty(maps)){
            return  R.error(ResponseCode.NO_RESPONSE_DATA.getMessage());
        }
        return R.ok(maps);
    }
    /**
     * 沪深两市版块分时行情数据查询，以交易时间和交易金额降序查询，去前10条数据
     *
     * @return R<List<StockBlockRtInfo>>
     * @author wangxue
     * @Date 2022/5/20
     */
    @Override
    public R<List<StockBlockRtInfo>> sectorAllLimit() {
        //1.调用毛片儿接口获取数据 TODO：优化 避免全表查询 根据时间范围查询，提高查询效率
        List<StockBlockRtInfo> infos =  stockBlockRtInfoMapper.sectorAllLimit();
        //2.组装数据
        if (CollectionUtils.isEmpty(infos)){
            return R.error(ResponseCode.NO_RESPONSE_DATA.getMessage());
        }
        return R.ok(infos);
    }

    /**
     * 统计沪深两市个股最新交易数据，并按涨幅降序排序查询前10条数据
     * @return R<List<StockUpDownDomain>>
     * @author wangxue
     * @Date 2022/5/24
     */
    @Override
    public R<List<StockUpDownDomain>> stockIncreaseLimit() {
        //1.直接调用mapper查询当前10的数据 TODO 以时间顺序取前10
        // 优化：
        //获取当前最近有效时间
        Date curDateTime = DateTimeUtil.getLastDate4Stock(DateTime.now()).toDate();
        //mock数据
        String mockStr = "2021-12-27 09:47:00";
        curDateTime = DateTime.parse(mockStr,DateTimeFormat.forPattern("yyy-MM-dd HH:mm:ss")).toDate();
        List<StockUpDownDomain> infos = stockRtInfoMapper.stockIncreaseLimit(curDateTime);
        //2.判断是否有数据
        if (CollectionUtils.isEmpty(infos)){
            return R.error(ResponseCode.NO_RESPONSE_DATA.getMessage());
        }
        return  R.ok(infos);
    }

    /**
     * 沪深两市各股行情列表查询，以时间顺序和涨幅分页查询
     * @param page 当前页
     * @param pageSize  每页大小
     * @return R<List<StockUpDownDomain>>
     * @author wangxue
     * @Date 2022/5/24
     */
    @Override
    public R<PageResult<StockUpDownDomain>> stockPage(Integer page, Integer pageSize) {
        //1.设置分页参数
        PageHelper.startPage(page,pageSize);
        //2.通过mapper查询
        List<StockUpDownDomain> infos = stockRtInfoMapper.stockAll();
        if (CollectionUtils.isEmpty(infos)){
            return R.error("暂无数据");
        }
        //3.封装到pageResult下
        //3.1 封装pageInfo对象
        PageInfo<StockUpDownDomain> listPageInfo = new PageInfo<>(infos);
        //3.2 将pageInfo对象转化为pageResult
        PageResult<StockUpDownDomain> pageResult = new PageResult<>(listPageInfo);
        //4.封装R响应对象
        return R.ok(pageResult);
    }

    /**
     * 沪深两市涨跌停分时行情数据查询，查询T日每分钟的涨跌停数据（T：当前股票交易日）
     * 		查询每分钟的涨停和跌停的数据的同级；
     * 		如果不在股票的交易日内，那么就统计最近的股票交易下的数据
     * 	 map:
     * 	    upList:涨停数据统计
     * 	    downList:跌停数据统计
     * @return R<Map>
     * @author wangxue
     * @Date 2022/5/24
     */
    @Override
    public R<Map> upDownCount() {

        //1.获取股票最近的有效交易日期，精确到秒
        DateTime curDateTime = DateTimeUtil.getLastDate4Stock(DateTime.now());
        //当前最近有效期
        Date curTime = curDateTime.toDate();
        //开盘日期
        Date openTime = DateTimeUtil.getOpenDate(curDateTime).toDate();
        //TODO mock_data 后续数据实现实时获取，注释掉
        String curTimeStr = "20220106142500";
        //对应开盘日期 mock_data
        String openTimeStr = "20220106092500";
        curTime = DateTime.parse(curTimeStr,DateTimeFormat.forPattern("yyyyMMddHHmmss")).toDate();
        openTime = DateTime.parse(openTimeStr,DateTimeFormat.forPattern("yyyyMMddHHmmss")).toDate();
        //2.统计涨停数据 约定：1：涨停 0：跌停
        List<Map> upCount = stockRtInfoMapper.upDownCount(curTime,openTime,1);
        //3.统计跌停的数据
        List<Map> downCount  = stockRtInfoMapper.upDownCount(curTime,openTime,0);
        //4.组装数据到map
        HashMap<String,List<Map>> info = new HashMap<>();
        info.put("upList",upCount);
        info.put("downList",downCount);
        //5.响应
        return R.ok(info);
    }
    /**
     * 将指定页的股票数据导出到Excel表下
     * @param response
     * @param page 当前页
     * @param pageSize 每页大小
     * @return
     * @author wangxue
     * @Date 2022/5/26
     */
    @Override
    public void stockExport(HttpServletResponse response, Integer page, Integer pageSize) {
        try {
            //1.设置响应数据的类型：excel
            response.setContentType("application/vnd.ms-excel");
            //2.设置相应数据的编码格式
            response.setCharacterEncoding("utf-8");
            //3.设置默认的文件名
            String fileName = URLEncoder.encode("stockRt","UTF-8");
            //设置默认文件名称
            response.setHeader("content-disposition","attachment;filename="+fileName+".xlsx");
            //4.分页查询股票数据
            PageHelper.startPage(page,pageSize);
            List<StockUpDownDomain> infos = this.stockRtInfoMapper.stockAll();
            Gson gson = new Gson();//主要用途为序列化Java对象为JSON字符串，或反序列化JSON字符串成Java对象。
            List<StockExcelDomain> excelDomains = infos.stream().map(info -> {
                StockExcelDomain domain = new StockExcelDomain();
                BeanUtils.copyProperties(info,domain);//可以快速的将source对象中的属性赋值给target对象
                return domain;
            }).collect(Collectors.toList());//Collectors.toList() 将流中的所有元素导出到一个列表( List )中
            //流式操作可进行很多的操作
            //过滤 fliter
            //运算map
            //去重distinct
            //……
            //经过N多的运算、筛选、排序、操作……返回我们需要的集合
            //5.导出
            EasyExcel.write(response.getOutputStream(),StockExcelDomain.class).sheet("股票数据").doWrite(excelDomains);
        } catch (IOException e) {
            log.info("股票excel数据导出异常，当前页：{}，每页大小：{}，异常信息：{}",page,pageSize,e.getMessage());
        }

    }

    /**
     * 统计国内A股大盘T日和T-1日成交量对比功能（成交量为沪市和深市成交量）
     *   map结构示例：
     *      {
     *         "volList": [{"count": 3926392,"time": "202112310930"},......],
     *       "yesVolList":[{"count": 3926392,"time": "202112310930"},......]
     *      }
     * @return * @return R<Map>
     * @author wangxue
     * @Date 2022/5/29
     */
    @Override
    public R<Map> stockTradeVol4InnerMarket() {

        //1.获取最近的股票交易日时间，精确到分钟，T交易日
        DateTime tDate = DateTimeUtil.getLastDate4Stock(DateTime.now());
        Date tDateTime = tDate.toDate();
        //对应的开盘时间
        Date tOpenTime = DateTimeUtil.getOpenDate(tDate).toDate();
        //TODO 后续注释掉mock-data
        String tDateStr = "20220103143000";
        tDateTime = DateTime.parse(tDateStr,DateTimeFormat.forPattern("yyyyMMddHHmmss")).toDate();
        //mock-data
        String openDateStr = "20220103093000";
        tOpenTime = DateTime.parse(openDateStr,DateTimeFormat.forPattern("yyyyMMddHHmmss")).toDate();
        //获取T-1交易日
        DateTime preTDate = DateTimeUtil.getPreviousTradingDay(tDate);
        Date preTTime = preTDate.toDate();
        Date preTOpenTime = DateTimeUtil.getOpenDate(preTDate).toDate();
        //TODO 后续注释掉mock-data
        String preTStr = "20220102143000";
        preTTime = DateTime.parse(preTStr,DateTimeFormat.forPattern("yyyyMMddHHmmss")).toDate();
        //mock-data
        String openDateStr2 = "20220102093000";
        preTOpenTime = DateTime.parse(openDateStr2,DateTimeFormat.forPattern("yyyyMMddHHmmss")).toDate();
        //2.获取到T日的过泡大盘交易统计数据
        List<Map> tData = stockMarketIndexInfoMapper.stockTradeVolCount(stockInfoConfig.getInner(),tOpenTime,tDateTime);
        //3.获取T-1的数据
        List<Map> preData = stockMarketIndexInfoMapper.stockTradeVolCount(stockInfoConfig.getInner(),preTOpenTime,preTTime);
        //4.组装数据
        HashMap<String,List<Map>> data = new HashMap<>();
        data.put("volList",tData);
        data.put("yesVolList",preData);
        return R.ok(data);
    }

    /**
     * 统计在当前时间下（精确到分钟），股票在各个涨跌区间的数量
     * 如果当前不在股票有效时间内，则以最近的一个有效股票交易时间作为查询时间点；
     * @return R<Map>
     *  响应数据格式：
     *  {
     *     "code": 1,
     *     "data": {
     *         "time": "2021-12-31 14:58:00",
     *         "infos": [
     *             {
     *                 "count": 17,
     *                 "title": "-3~0%"
     *             },
     *             //...
     *             ]
     *     }
     * @author wangxue
     * @Date 2022/5/30
     */
    @Override
    public R<Map> stockUpDownScopeCount() {
        //1.获取当前时间下最近的一个股票交易时间 精确到秒
        DateTime avlDateTime = DateTimeUtil.getLastDate4Stock(DateTime.now());
        Date avlDate = avlDateTime.toDate();
        //TODO 后续删除 mock-data
        String  mockDate="20220106095500";
        avlDate = DateTime.parse(mockDate, DateTimeFormat.forPattern("yyyyMMddHHmmss")).toDate();
        //2.查询
        List<Map> maps=stockRtInfoMapper.stockUpDownScopeCount(avlDate);
        //获取去股票涨幅区间的集合
        List<String> upDownRange = stockInfoConfig.getUpDownRange();//获取到对应的yml中设置的map顺序
        //将list集合下的字符串映射成Map对象
        List<Map> orderMap = upDownRange.stream().map(key -> {
            Optional<Map> title = maps.stream().filter(map -> key.equals(map.get("title"))).findFirst();
            //判断对应的map是否存在
            Map tmp = null;
            if (title.isPresent()) { //isPresent()判断查询的类对象是否存在
                tmp = title.get();
            } else {
                tmp = new HashMap();
                tmp.put("title", key);
                tmp.put("count", 0);
            }
            return tmp;
        }).collect(Collectors.toList());
        //3.组装data
        HashMap<String, Object> data = new HashMap<>();
        data.put("time",avlDateTime.toString("yyyy-MM-dd HH:mm:ss"));
        data.put("infos",orderMap);
        //返回响应数据
        return R.ok(data);
    }

    /**
     *查询单个个股的分时行情数据，也就是统计指定股票T日每分钟的交易数据；
     *         如果当前日期不在有效时间内，则以最近的一个股票交易时间作为查询时间点
     * @param code 股票编码
     * @return  R<List<Stock4MinuteDomain>>
     * @author wangxue
     * @Date 2022/5/31
     */
    @Override
    public R<List<Stock4MinuteDomain>> stockScreenTimeSharing(String code) {

        //1.获取最近有效的股票交易时间
        DateTime curDateTime = DateTimeUtil.getLastDate4Stock(DateTime.now());
        //获取当前时间
        Date curDate = curDateTime.toDate();
        //获取当前日期对应的开盘日期
        Date openDate = DateTimeUtil.getOpenDate(curDateTime).toDate();
        //TODO 后续删除 mock-data

        String mockDate = "20220106142500";
        curDate = DateTime.parse(mockDate,DateTimeFormat.forPattern("yyyyMMddHHmmss")).toDate();
        String openDateStr = "20220106093000";
        openDate  = DateTime.parse(openDateStr,DateTimeFormat.forPattern("yyyyMMddHHmmss")).toDate();
        List<Stock4MinuteDomain> maps = stockRtInfoMapper.stockScreenTimeSharing(code,openDate,curDate);
        //响应前端
        return R.ok(maps);
    }

    /**
     * 单个个股日K 数据查询 ，可以根据时间区间查询数日的K线数据
     *      默认查询历史20天的数据
     * @param code 股票编码
     * @author wangxue
     * @Date 2022/5/31
     */
    @Override
    public R<List<Stock4EvrDayDomain>> stockCreenDkLine(String code) {
        //获取当前日期前推20天
        DateTime curDateTime = DateTimeUtil.getLastDate4Stock(DateTime.now());
        //当前时间节点
        Date curTime = curDateTime.toDate();
        //前推20
        Date pre20Day = curDateTime.minusDays(20).toDate();

        //TODO 后续删除
        String avlDate="20220106142500";
        curTime=DateTime.parse(avlDate, DateTimeFormat.forPattern("yyyyMMddHHmmss")).toDate();
        String openDate="20220101093000";
        pre20Day=DateTime.parse(openDate, DateTimeFormat.forPattern("yyyyMMddHHmmss")).toDate();
//        List<Stock4EvrDayDomain> infos = stockRtInfoMapper.stockCreenDkLine(code, pre20Day, curTime);
        //获取指定范围的收盘日期集合
        List<Date> closeDates=stockRtInfoMapper.getCloseDates(code,pre20Day,curTime);
        //根据收盘日期精准查询，如果不在收盘日期，则查询最新数据
        List<Stock4EvrDayDomain> infos= stockRtInfoMapper.getStockCreenDkLineData(code,closeDates);
        return R.ok(infos);
    }


}
